package com.atguigu.crud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.atguigu.crud.bean.Wxmenu;
import com.atguigu.crud.service.WxmenuService;

/**
 * 微信菜单
 * @author Administrator
 *
 */
@RequestMapping("/weixinMenu")
@Controller
public class WeixinMenuController {

	@Autowired
	private WxmenuService wxmenuService;
	
	//列表
	@RequestMapping("/list")
	public String list(Model model){
		model.addAttribute("menus", wxmenuService.getAll());//数据库数据菜单
		model.addAttribute("wmds", wxmenuService.generateWeixinMenuDto());//微信接口结构
		return "admin/wxmenu/list";
	}

    //微信自定义菜单跳转页面
	@RequestMapping("/wmeunpage")
	public String wmeunpage(Model model){
		return "admin/wxmenu/menu";
	}
	
	//保存跳转页面
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String add(Model model) {
		model.addAttribute("menu", new Wxmenu());
		return "admin/wxmenu/add";
	}
	
	//保存
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(Wxmenu menu) {
		wxmenuService.saveWxmenu(menu);
		return "redirect:/weixinMenu/list";
	}
	
	//更新
	@RequestMapping(value="/update/{id}",method=RequestMethod.POST)
	public String update(Wxmenu menu) {
		wxmenuService.updateWxmenu(menu);
		return "redirect:/weixinMenu/list";
	}
	
	@RequestMapping(value="/update/{id}",method=RequestMethod.GET)
	public String update(@PathVariable int id,Model model) {
		model.addAttribute("menu", wxmenuService.getWxmenu(id));
		return "admin/wxmenu/update";
	}
	
	//删除
	@RequestMapping(value="/delete/{id}",method=RequestMethod.GET)
	public String delete(@PathVariable("id")int id) {
		wxmenuService.deleteWxmenu(id);
		return "redirect:/weixinMenu/list";
	}
	
	//查询微信发布菜单成功后的接口查询数据
	@RequestMapping("queryPublishMenu")
	public String queryPublish(Model model){
		model.addAttribute("ms", wxmenuService.queryMenu());
		return "admin/wxmenu/publish";
	}
	
	//调用微信发布菜单接口
	@RequestMapping("/publishMenu")
	public String publishMenu() {
		wxmenuService.publishMenu();
		return "redirect:/weixinMenu/queryPublishMenu";
	}
}
